// src/utils/http.ts
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
import { message } from 'antd'
import { getToken, loginOut } from '../hooks/userUserInfoStore'

// 类型定义
interface ResponseData<T = any> {
  code: number
  data?: T
  message?: string
}

interface CustomRequestConfig extends AxiosRequestConfig {
  hideErrorMessage?: boolean
}

// 创建axios实例
const createHttpInstance = () => {
  const instance = axios.create({
    baseURL: process.env.REACT_APP_API_BASE_URL,
    timeout: 10000,
    headers: { 'Content-Type': 'application/json' }
  })

  // 请求拦截
  instance.interceptors.request.use(config => {
    const token = getToken()
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  })

  // 响应拦截
  instance.interceptors.response.use(
    (response: AxiosResponse<ResponseData>) => handleResponse(response),
    error => handleHttpError(error)
  )

  return instance
}

// 处理响应
const handleResponse = (response: AxiosResponse) => {
  const res = response.data
  const config = response.config as CustomRequestConfig

  if (res.code === 200) {
    return res
  }

  const errorMsg = res.message || '请求处理失败'
  if (!config.hideErrorMessage) {
    message.error(errorMsg)
  }

  return Promise.reject(new Error(errorMsg))
}

// 处理HTTP错误
const handleHttpError = (error: any) => {
  const errorMessage = getHttpErrorMsg(error)
  const config = error.config as CustomRequestConfig | undefined

  if (!config?.hideErrorMessage) {
    message.error(errorMessage)
  }

  return Promise.reject(new Error(errorMessage))
}

// 获取HTTP错误信息
const getHttpErrorMsg = (error: any): string => {
  if (error.response) {
    switch (error.response.status) {
      case 401:
        setTimeout(() => {
          loginOut()
        }, 1000)
        return '登录已过期，请重新登录'
      case 403:
        return '拒绝访问'
      case 500:
        return '服务器内部错误'
      default:
        return error.response.data?.msg || '请求错误'
    }
  }
  return error.request ? '网络连接异常' : '未知错误'
}

// 创建实例并导出方法
const httpInstance = createHttpInstance()

export default <T = any>(config: CustomRequestConfig) =>
  httpInstance.request<T, T>(config)
